package com.zhoukx.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用前面学习后的ByteBuffer(缓冲) 和 FileChannel(通道)， 将 "hello,尚硅谷" 写入到file01.txt 中
 *
 */
public class NIOFilerChannerl02 {
    public static void main(String[] args) throws IOException {
        // 创建输入流
        File file = new File("d:\\file01.txt");
        // 获取管道
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel channel = fileInputStream.getChannel();
        // 创建buffer内存
        ByteBuffer allocate = ByteBuffer.allocate((int) file.length());
        // 读取
         channel.read(allocate);
        // 转换成String打印
        System.out.println(new String(allocate.array()));
        // 关闭流
        fileInputStream.close();
    }
}
